<?php
/**
 * Desc： 数据表格-头部
 * Author：Fly
 * DateTime：2020/9/12 11:45
 */

namespace FlyToper\PearBackend;

use FlyToper\PearBackend\headers\Basic;
use FlyToper\PearBackend\headers\CkField;

class Header
{
    protected $cols = [];
    protected $align = 'center';

    public function __construct(array $cols = [])
    {
        $this->cols = $cols;
    }

    public function align($v = 'center')
    {
        $this->align = $v;
        return $this;
    }

    /**
     * 生成表头配置
     * @return array
     */
    public function render()
    {
        $data = [];
        foreach ($this->cols as $field => $title) {
            if($title instanceof CkField) {
                $item = ['type' => 'checkbox'];
            }else if($title instanceof Basic) {
                $item = $title->conf;
                if(empty($item['align'])) {
                    $item['align'] = $this->align;//使用默认的
                }
                $item['field'] = $field;
                $item['title'] = $title->title;
            } else {
                $item = [
                    'field' => $field,
                    'title' => $title,
                    'align' => $this->align,
                ];
            }
            $data[] = $item;
        }
        return $data;
    }
}